package com.sky.controller.admin;

import com.aliyuncs.http.HttpResponse;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;


@RestController
@RequestMapping("/admin/report")
@Api(tags = "数据统计")
@Slf4j
public class ReportController {

    @Autowired
    ReportService reportService;

    @GetMapping("/turnoverStatistics")
    @ApiOperation("营业额度查询")
    public Result<TurnoverReportVO> getStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询 {} - {} 的营业额度", begin, end);
        TurnoverReportVO reportVO = reportService.getStatistics(begin, end);

        return Result.success(reportVO);
    }

    @GetMapping("/userStatistics")
    @ApiOperation("用户数据查询")
    public Result<UserReportVO> getUserStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询 {} - {} 的用户数据", begin, end);
        UserReportVO reportVO = reportService.getUserStatistics(begin, end);

        return Result.success(reportVO);
    }

    @GetMapping("/ordersStatistics")
    @ApiOperation("订单数据查询")
    public Result<OrderReportVO> getOrdersStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询 {} - {} 的订单数据", begin, end);
        OrderReportVO reportVO = reportService.getOrdersStatistics(begin, end);

        return Result.success(reportVO);
    }

    @GetMapping("/top10")
    @ApiOperation("菜品排名数据查询")
    public Result<SalesTop10ReportVO> getTopDish(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询 {} - {} 的菜品排名数据", begin, end);
        SalesTop10ReportVO reportVO = reportService.getTopDish(begin, end);

        return Result.success(reportVO);
    }

    @GetMapping("/export")
    @ApiOperation("导出报表")
    public void export(HttpServletResponse response) {
        reportService.export(response);
    }
}
